import datetime
import random
import time

from common.com_time import Time
import numpy as np
import uuid
import hashlib


class Utils(object):

    @classmethod
    def md5(cls, text: str = None, slat: str = None) -> str:
        """
        对字符串加密
        :param text: 加密的字符串
        :param slat: 加盐字符串
        :return:
        """
        if text is not None:
            if slat is not None:
                return hashlib.md5(f"{slat}{text}".encode('utf-8')).hexdigest()
            else:
                return hashlib.md5(f"{text}".encode('utf-8')).hexdigest()
        else:
            if slat is not None:
                return hashlib.md5(f"{slat}".encode('utf-8')).hexdigest()
            else:
                return hashlib.md5().hexdigest()

    @classmethod
    def random_num_generator(cls, random_num: int = 8):
        """
        随机生成指定长度的数字
        :param random_num: 随机数字
        :return:
        """
        return np.random.randint(low=10 ** (random_num - 1), high=10 ** random_num, size=1, dtype='int64')[0]

    @classmethod
    def random_code_generator(cls, default_letter: str = "", conn_sym: str = "", num_len: int = 6) -> str:
        """
        随机代码生成器
        :param default_letter: 默认字母
        :param conn_sym: 连接符号
        :param num_len: 数字部分长度
        :return:
        """
        return f"{default_letter}{conn_sym}{cls.random_num_generator(random_num=num_len)}"

    @classmethod
    def generator_post_code(cls, num_len: int = 6):
        """
        随机生成职位代码
        :param num_len: 代码数字部分长度
        :return:
        """
        return cls.random_code_generator(default_letter="PC", conn_sym="-", num_len=num_len)

    @classmethod
    def generator_live_platform_code(cls, num_len: int = 10):
        """
        随机生成直播平台代码
        :param num_len: 代码数字部分长度
        :return:
        """
        return cls.random_code_generator(default_letter="LPC", conn_sym="-", num_len=num_len)

    @classmethod
    def generator_pk_mode_code(cls, random_len: int = 5):
        """
        随机生成竞赛代码代码
        :param random_len: 随机数字长度
        :return:
        """
        _current_str = str(datetime.datetime.now()).replace('-', '', 5).replace(
            ' ', '', 5).replace(':', '', 5).replace('.', '', 5)
        _random_num = cls.random_num_generator(random_num=random_len)
        return f"PMC-{_current_str}-{_random_num}"

    @classmethod
    def generator_live_room_number(cls, num_len: int = 30):
        """
        随机生成直播间号码
        :param num_len: 代码数字部分长度
        :return:
        """
        return ''.join(random.choices(cls.md5(text=Time.current_datetime_str()), k=num_len))

    @classmethod
    def generator_subject_code(cls, num_len: int = 6):
        """
        随机生成学科代码
        :param num_len: 代码数字部分长度
        :return:
        """
        return cls.random_code_generator(default_letter="SC", conn_sym="-", num_len=num_len)

    @classmethod
    def generator_label_code(cls, num_len: int = 8):
        """
        随机生成标签代码
        :param num_len: 代码数字部分长度
        :return:
        """
        return cls.random_code_generator(default_letter="LC", conn_sym="-", num_len=num_len)

    @classmethod
    def generator_streamer_code(cls, num_len: int = 20):
        """
        随机生成主播代码
        :param num_len: 代码数字部分长度
        :return:
        """
        return ''.join(random.choices(cls.md5(text=Time.current_datetime_str()), k=num_len))

    @classmethod
    def generator_player_code(cls, num_len: int = 20):
        """
        随机生成玩家代码
        :param num_len: 代码数字部分长度
        :return:
        """
        return ''.join(random.choices(cls.md5(text=Time.current_datetime_str()), k=num_len))


if __name__ == '__main__':
    print(Utils.generator_post_code())
    print(Utils.generator_live_platform_code())
    print(Utils.generator_pk_mode_code())
    print(Utils.generator_live_room_number())
    print(Utils.generator_subject_code())
    print(Utils.generator_label_code())
    print(Utils.generator_streamer_code())
    print(Utils.generator_player_code())
    print(Utils.md5())
